﻿using DevExpress.Utils;
using DevExpress.XtraBars.Alerter;
using Inaction.UI.Common;
using Inaction.UI.Extensions.BarExtensions;
using Inaction.UI.Extensions.ControlExtensions;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Inaction.UI.Extensions
{
    public static class ExtensionHelper
    {
        public static event Action<Control, Form> ControlExtendAction;
        public static event Action<Form> FormExtendAction;

        static System.Threading.SynchronizationContext syncContext = System.Threading.SynchronizationContext.Current;

        #region 方法
        internal static void RegeistExtenders()
        {
            BarManager.InitExtendAction += LayoutExtender.Extend;
            BarManager.InitExtendAction += BindingEditorExtender.Extend;
            BarManager.ShowContextMenuExtendAction += ResetLayoutExtender.Extend;

            ControlExtendAction += LayoutControlExtender.Extend;
            ControlExtendAction += GridExtender.Extend;
            ControlExtendAction += LookUpEditBaseExtender.Extend;
            ControlExtendAction += LookUpEditExtender.Extend;
            ControlExtendAction += GridLookUpEditExtender.Extend;
        }

      
        internal static void ExtendControls(Form form)
        {
            if (TagFormList.Contains(form.GetHashCode()) ) return;

            if (form is WaitDialogForm) return;

            TagFormList.Add(form.GetHashCode());
            if (form.Modal || !form.IsMdiChild)
            {
                form.Icon = ExtensionHost.MainIcon;
                form.ShowIcon = true;
            }

            form.Disposed += (sender, e) => { TagFormList.Remove(form.GetHashCode()); };

            var thread = new System.Threading.Thread(
                (object state) =>
                {
                    var sync = state as System.Threading.SynchronizationContext;
                    if (FormExtendAction != null)
                    {
                        sync.Post((obj) => { FormExtendAction(form); }, null);
                    }
                    if (ControlExtendAction != null)
                    {
                        List<Control> list;
                        if (form is BaseForm && (form as BaseForm).BarManager != null)
                        {
                            list = (form as BaseForm).BarManager.FormManager.Controls;
                        }
                        else
                        {
                            list = ControlHelper.FindControls<Control>(form);
                        }
                        foreach (Control c in list)
                        {
                            sync.Post((obj) => { ControlExtendAction(c, form); }, null);
                        }
                    }
                }
            );
            thread.Start(syncContext);
        }

        private static List<int> TagFormList = new List<int>();

        public static IList<System.Type> GetBindingDataTypes(Form form)
        {
            if (types.ContainsKey(form))
                return types[form];
            var list = new List<System.Type>();
            BarManager barmager = BarManager.GetBarManager(form);
            if (barmager == null) return list;
            foreach (var binding in barmager.BindingInfos)
            {
                if (binding.RootSource != barmager.MasterBindingInfo.BindingSource) continue;
                var type = BindingHelper.GetListItemType(binding.BindingSource);
                if (type != null)
                {
                    list.Add(type);
                }
            }
            types.Add(form, list);
            return list;
        }

        private static readonly Dictionary<Form, IList<System.Type>> types = new Dictionary<Form, IList<Type>>();
        #endregion
    }
}
